/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.customer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.customer.Village;
import src.services.customer.SubdistrictModel;
import src.services.customer.VillageModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class VillageBean implements Serializable {

    private List<Village> villages;
    private List<SelectItem> avaibleSubdistrict;
    private Village selectedData, insertedData;
    private boolean editMode = true;
    private String filter;
    private SubdistrictModel districtHelper;

    public VillageBean() {
        VillageModel helper = new VillageModel();
        this.villages = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Village> getVillages() {
        return this.villages;
    }
    
    public Village getSelectedData() {
  //      System.out.println(">>>");
        return selectedData;
    }

    public void setSelectedData(Village selectedData) {
        this.selectedData = selectedData;
    }
    
    public Village getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Village insertedData) {
        this.insertedData = insertedData;
    }
    
    public List<SelectItem> getAvaibleSubdistrict() {
        return this.avaibleSubdistrict;
    }
    
    public void setAvaibleSubdistrict(List<SelectItem> avaibleSubdistrict) {
        this.avaibleSubdistrict = avaibleSubdistrict;
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    public void search() {
        Map<String, String> filters = new HashMap<>();
      
        filters.put("subdistrictName", getFilter());
         VillageModel helper = new VillageModel();      
        this.villages = helper.findAll(filters);
        setFilter("");
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.districtHelper = null;
        this.editMode = true;      
    }
    
    public void initForm() {
        districtHelper = new SubdistrictModel();
        this.avaibleSubdistrict = districtHelper.listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.insertedData = Village.clone(selectedData);
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Village();
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        VillageModel helper = new VillageModel();
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.villages.add(insertedData);
            else {
                int i = villages.indexOf(selectedData);
                if (i <= villages.size()) villages.set(i, Village.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setVillageId(null);
        } 
        
        /*boolean success;
             
        Village data = this.insertedData;
        if (this.insertedData == null) data = this.selectedData;
        VillageModel helper = new VillageModel();
        success = helper.saveData(data);
        if (success && this.editMode!=true) {
            this.villages.add(this.insertedData);
        }    
        
        * 
        */
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;
        VillageModel helper = new VillageModel();
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.villages.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
}
